package edu.siu.math.egut.functions;

import java.util.ArrayList;
import java.util.List;

import edu.siu.math.egut.lie.AbstractEgutFunction;
import edu.siu.math.egut.lie.LiEGroup;
import edu.siu.math.egut.lie.LiEMatrix;
import edu.siu.math.egut.lie.LiEText;
import edu.siu.math.egut.lie.LiEType;
import edu.siu.math.egut.util.ExceptionalGroup;
import edu.siu.math.egut.util.Polynomial;
import edu.siu.math.egut.util.OneParameterSubGroup;
import edu.siu.math.egut.util.ExceptionalGroupElement;

/**
 * This is used to convert dvu to du'.
 * The format of this command is:
 * egutKillingMachine(mat d, mat u, mat v, mat inv, grp group).
 * 
 * @author Joe Pleso
 *
 */
public class KillingMachine extends AbstractEgutFunction {

    
    public LiEType[] argList() {
	LiEType[] result = new LiEType[5];
	    result[0]=LiEType.MAT;
	    result[1]=LiEType.MAT;
	    result[2]=LiEType.MAT;
	    result[3]=LiEType.MAT;
	    result[4]=LiEType.GRP;
	    return result;
    }

    
    public void run() {

	final String D = "d";
	final String U = "u";
	final String V = "v";
	String[] args = this.getRawData();
	
	int[][] d = (int[][]) new LiEMatrix(args[0]).getJavaObject();
	int[][] u = (int[][]) new LiEMatrix(args[1]).getJavaObject();
	int[][] v = (int[][]) new LiEMatrix(args[2]).getJavaObject();
	int[][] inv = (int[][]) new LiEMatrix(args[3]).getJavaObject();
	ExceptionalGroup rs = (ExceptionalGroup) new LiEGroup(args[4]).getJavaObject();
	
	if (rs == null){//Check to see that we really have a group
		args[0] = "\"Specified group was not exceptional.\"";
		this.setData(args);
		return;
	    }
	
	List<OneParameterSubGroup> uTmp = new ArrayList<OneParameterSubGroup>();
	for (int i = 0; i < d.length; i++){
	    
	    uTmp.add(OneParameterSubGroup.create(d[i], 
		    Polynomial.create(D + Integer.toString(i))));
	}
	for (int i = 0; i < v.length; i++){
	    
	    uTmp.add(OneParameterSubGroup.create(v[i], 
		    Polynomial.create(V + Integer.toString(i))));
	}
	for (int i = 0; i < u.length; i++){
	    
	    uTmp.add(OneParameterSubGroup.create(u[i], 
		    Polynomial.create(U + Integer.toString(i))));
	}
	
	ExceptionalGroupElement uni = new ExceptionalGroupElement(rs,uTmp);
	
	int j = uni.indexOf(v);
	while (j != -1){
	    uni.pushLeft(j);
	    if (uni.indexOf(inv) == 0)
		uni.remove(0);
	    j = uni.indexOf(v);
	}
	args[0] = new LiEText(uni.toString()).toString();
	this.setData(args);

    }

}
